<?php 
## include required files
/*******************************/
require_once '../../includefiles.php';
include_once("../../model/users.php");
include_once("../../model/trade.php");
include_once("../../model/leads.php");
include_once("../../model/state.php");
require_once '../../model/city.php';
include_once("../../model/budget.php");
include_once '../../model/paypal.php';
include_once '../../model/nab.php';
include_once("../../model/usermembership.php");
include_once("../../model/usermembershipdetail.php");
require_once '../../model/membershipplan.php';
require_once '../../model/admindetails.php';
include_once '../../model/email.php';
require_once '../../includes/classes/paypalfunction.php'; // Pay Pal pro Function file 
require_once '../../redirect.php';
$paypalObj = new Model_Paypal();
$nabObj = new Model_NAB();
$userObj = new Model_Users();
$userId = $_SESSION['getbids_id'];
$userDetArray = $userObj->getUserDetailsByUserId($userId);
$nab = $nabObj->getDetail();

$getRandonUpper 						= 		strtoupper(getRandomNumber());
$nextOrder = $nabObj->getNextOrder();
$invoiceId	= processOrderId($nextOrder, $nab['invoice_format']);


checkUserLogin();


if(!$_SESSION['cats']['list'] || !$_SESSION['cats']['subtotal'])
{
	@header("location:".SITE_URL."/myaccount/myservicelocation/");
	exit;
}


if(!$_POST['paymentmethod'])
{
	redirect_to_link(SITE_URL);
	exit;	
}	
else
{
	$cats = $_SESSION['cats'];	
	$_SESSION['cats']['payment'] = $_POST;
	$type = "Categories purchasing";
	$finalTotal 	= $cats["total"];
	$amount 		= $finalTotal;	
	$_SESSION['cats']['invoice'] = $invoiceId;
	
	switch($_SESSION['cats']["payment"]['paymentmethod'])
	{
		case 'nab':
			

			date_default_timezone_set('GMT');
			$date = date('YmdHis');

			if($nab['mode'])
			{
				$url = $nab['live_url'];
				$furl = "https://transact.nab.com.au/live/directpost/genfingerprint?";
			}
			else
			{
				$url = $nab['test_url'];
				$furl = "https://transact.nab.com.au/test/directpost/genfingerprint?";

			}
			
			//get fingerprint
			$furl.= "EPS_MERCHANT=".$nab['merchant_id'];
			$furl.= "&EPS_PASSWORD=".$nab['merchant_password'];
			$furl.= "&EPS_AMOUNT=".$amount;
			$furl.= "&EPS_REFERENCEID=".$invoiceId;
			$furl.= "&EPS_TIMESTAMP=".$date;			
			$fingerprint = getFingerprint($furl);
			
			$post_variables = Array(
			'EPS_MERCHANT'      => $nab['merchant_id'],
			/*'EPS_PASSWORD'      => $nab['merchant_password'],*/
			'EPS_REFERENCEID'   => $invoiceId, 
			'EPS_AMOUNT'    	=> $amount, 
			'EPS_TIMESTAMP'     => $date,
			"EPS_FINGERPRINT"   => $fingerprint,
			'EPS_CARDTYPE'      => $_SESSION['cats']['payment']['cardtype'],
			"EPS_CARDNUMBER"    => $_SESSION['cats']['payment']['cardnumber'],
			"EPS_EXPIRYMONTH"   => $_SESSION['cats']['payment']['cardmonth'],
			"EPS_EXPIRYYEAR"    => $_SESSION['cats']['payment']['carddate'],
			"EPS_CCV"         	=> $_SESSION['cats']['payment']['cardcode'],
			"EPS_RESULTURL"     => SITE_URL .'?pre=myaccount' ,
			);
			$smarty->assign('url',$url);
			$smarty->assign('payvars',$post_variables);
			$smarty->display(TEMPLATEDIR.'/modules/myaccount/gotopayment_nab.tpl');
		break;
		case 'paypal':
		default:
		
			$paypals = $paypalObj->getDetailsFromPaypal();
			$paypal = $paypals[0];
			if($paypal['mode']) //live
			{
				$url = $paypal['url_live'];
				$merchant = $paypal['merchant_live'];
			}
			else
			{
				$url = $paypal['url_sandbox'];
				$merchant = $paypal['merchant_sandbox'];
			}



			$post_variables = Array(
			'cmd'               => '_ext-enter',
			'redirect_cmd'      => '_xclick',
			'business'          => $merchant, //Email address or account ID of the payment recipient (i.e., the merchant).
			'receiver_email'    => $merchant, //Primary email address of the payment recipient (i.e., the merchant
			'order_number'      => $invoiceId.rand(0,1000),
			"invoice"           => $invoiceId.rand(0,1000),
			'item_name'         => 'Payment for Get-bids.com.au',
			//'quantity'          => $quantity,
			"amount"            => $amount,
			"currency_code"     => 'AUD',
			"first_name"        => $userDetArray['first_name'],
			"last_name"         => $userDetArray['last_name'],
			"address1"          => $_POST['billingaddr'],
			"zip"               => $_POST['cardpostalcode'],
			"city"              => $city,
			"state"             => $stateID_code,
			"country"           => 'AU',
			"email"             => $userDetArray['email'],
			"return"            => SITE_URL .'/myaccount/paymentreturn/',
			"notify_url"        => SITE_URL .'/myaccount/paymentnotify/',
			"cancel_return"     => SITE_URL .'/myaccount/paymentcancel/',
			"rm"				=> 2
			);
			$smarty->assign('url',$url);
			$smarty->assign('payvars',$post_variables);
			$smarty->display(TEMPLATEDIR.'/modules/myaccount/gotopayment_paypal.tpl');
		break;	
	}
	
	unset($smarty);
			
}
function getFingerprint($url)
{
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
		curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
		curl_setopt($ch, CURLOPT_HEADER, false);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_POST, true);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
		$str_token = curl_exec($ch);
		curl_close ($ch);
		return $str_token;

}

function processOrderId($id,$str)
{
	$num=$id;
	if($id<999)
		$num= '0'.$id;
	if($id<99)
		$num= '00'.$id;
	if($id<9)
		$num= '000'.$id;
	
	return str_replace("XXXX",$num,$str);
	
}

?>

